package com.jiechuang.user.controller;


import com.github.pagehelper.PageInfo;
import com.jiechuang.common.annotation.JWTPermission;
import com.jiechuang.common.bean.Permission;
import com.jiechuang.common.bean.Role;
import com.jiechuang.common.bean.addOrUpdate.RoleAddOrUpdate;
import com.jiechuang.common.web.Code;
import com.jiechuang.common.web.Response;
import com.jiechuang.user.dto.RoleQueryInfo;
import com.jiechuang.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @PostMapping("/list")
    @JWTPermission(role = "role")
    public Response roleList(@RequestBody RoleQueryInfo roleQueryInfo){
        try {
            PageInfo<Role> pageInfo = roleService.getRoleList(roleQueryInfo);
            return new Response(Code.SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PostMapping("/role")
    public Response addRole(@RequestBody RoleAddOrUpdate roleAddOrUpdate){
        try {
             roleService.addRole(roleAddOrUpdate);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SAVE_ERROR);
        }
    }
    @DeleteMapping("/{id}")
    public Response deleteRole(@PathVariable Integer id){
        try {
            roleService.deleteRole(id);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PutMapping("/role")
    @JWTPermission(role = "role")
    public Response updateRole(@RequestBody RoleAddOrUpdate roleAddOrUpdate){
        try {
            roleService.updateRole(roleAddOrUpdate);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @GetMapping("/{id}")
    @JWTPermission(role = "role")
    public Response getRole(@PathVariable Integer id){
        try {
            Role role=roleService.getRole(id);
            return new Response(Code.SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @DeleteMapping("/{roleId}/permission/{permissionId}")
    public Response deletePermissionByRoleId(@PathVariable Integer roleId,@PathVariable Integer permissionId){
        try {
            List<Permission> permissionList=roleService.deletePermissionByRoleId(roleId,permissionId);
            return new Response(Code.SUCCESS,permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PostMapping(path = {"/{roleId}/permission/{permissionIds}","/{roleId}/permission/"})
    public Response givePermissionByRoleId(@PathVariable Integer roleId,@PathVariable(required = false) List<Integer> permissionIds){
        try {
            roleService.givePermissionByRoleId(roleId,permissionIds);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }

}
